<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="google-site-verification" content="xBT4GhYoi5qRD5tr338pgPM5OWHHIDR6mNg1a3euekI" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="陌无崖的博客">
    <meta name="keyword"  content="">
    <link rel="shortcut icon" href="/img/favicon.ico">

    <title>
        
        顺序打印矩阵 - 陌无崖的博客 | mowuya&#39;s Blog
        
    </title>

    <!-- Custom CSS -->
    <link rel="stylesheet" href="/css/aircloud.css">
    <link rel="stylesheet" href="/css/gitment.css">
    <!--<link rel="stylesheet" href="https://imsun.github.io/gitment/style/default.css">-->
    <link href="//at.alicdn.com/t/font_620856_pl6z7sid89qkt9.css" rel="stylesheet" type="text/css">
    <!-- ga & ba script hoook -->
    <script></script>
</head>

<body>

<div class="site-nav-toggle" id="site-nav-toggle">
    <button>
        <span class="btn-bar"></span>
        <span class="btn-bar"></span>
        <span class="btn-bar"></span>
    </button>
</div>

<div class="index-about">
    <i> What Should I Do？ </i>
</div>

<div class="index-container">
    
    <div class="index-left">
        
<div class="nav" id="nav">
    <div class="avatar-name">
        <div class="avatar ">
            <img src="/img/avatar.jpg" />
        </div>
        <div class="name">
            <i>陌无崖</i>
        </div>
    </div>
    <div class="contents" id="nav-content">
        <ul>
            <li >
                <a href="/">
                    <i class="iconfont icon-shouye1"></i>
                    <span>主页</span>
                </a>
            </li>
            <li >
                <a href="/tags">
                    <i class="iconfont icon-biaoqian1"></i>
                    <span>标签</span>
                </a>
            </li>
            <li >
                <a href="/archive">
                    <i class="iconfont icon-guidang2"></i>
                    <span>存档</span>
                </a>
            </li>
            <li >
                <a href="/about/">
                    <i class="iconfont icon-guanyu2"></i>
                    <span>关于</span>
                </a>
            </li>
            
            <li>
                <a id="search">
                    <i class="iconfont icon-sousuo1"></i>
                    <span>搜索</span>
                </a>
            </li>
            
        </ul>
    </div>
    
        <div id="toc" class="toc-article">
    <ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#顺时针打印矩阵"><span class="toc-text">顺时针打印矩阵</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#思路"><span class="toc-text">思路</span></a></li></ol>
</div>
    
</div>


<div class="search-field" id="search-field">
    <div class="search-container">
        <div class="search-input">
            <span id="esc-search"> <i class="icon-fanhui iconfont"></i></span>
            <input id="search-input"/>
            <span id="begin-search">搜索</span>
        </div>
        <div class="search-result-container" id="search-result-container">

        </div>
    </div>
</div>

        <div class="index-about-mobile">
            <i> What Should I Do？ </i>
        </div>
    </div>
    
    <div class="index-middle">
        <!-- Main Content -->
        


<div class="post-container">
    <div class="post-title">
        顺序打印矩阵
    </div>

    <div class="post-meta">
        <span class="attr">发布于：<span>2020-03-02 18:51:31</span></span>
        
        <span class="attr">标签：/
        
        <a class="tag" href="/tags/#算法" title="算法">算法</a>
        <span>/</span>
        
        
        </span>
        <span class="attr">访问：<span id="busuanzi_value_page_pv"></span>
</span>
</span>
    </div>
    <div class="post-content ">
        <h3 id="顺时针打印矩阵"><a href="#顺时针打印矩阵" class="headerlink" title="顺时针打印矩阵"></a>顺时针打印矩阵</h3><figure class="highlight basic"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="symbol">1 </span> <span class="number">2</span>  <span class="number">3</span>  <span class="number">4</span></span><br><span class="line"><span class="symbol">5 </span> <span class="number">6</span>  <span class="number">7</span>  <span class="number">8</span></span><br><span class="line"><span class="symbol">9 </span> <span class="number">10</span> <span class="number">11</span> <span class="number">12</span></span><br><span class="line"><span class="symbol">13 </span><span class="number">14</span> <span class="number">15</span> <span class="number">16</span></span><br></pre></td></tr></table></figure>
<h3 id="思路"><a href="#思路" class="headerlink" title="思路"></a>思路</h3><p><strong><em>定义四个边界</em></strong></p>
<p>打印顺序：</p>
<ul>
<li>1,2,3  右边界至3</li>
<li>4,8 12 底部边界至12</li>
<li>16 15 14 左边界至14</li>
<li>13 9 5 上边界至5</li>
</ul>
<p>每次经过一个循环，各个边界进行自增或自减操作</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 顺时针打印矩阵</span></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">PrintMatrixClockwisely</span><span class="params">(data [][]<span class="keyword">int</span>)</span></span> &#123;</span><br><span class="line">	<span class="comment">// 定义四个边界</span></span><br><span class="line">	<span class="comment">// 右边界</span></span><br><span class="line">	right := <span class="built_in">len</span>(data[<span class="number">0</span>]) - <span class="number">2</span></span><br><span class="line">	<span class="comment">// 底部边界</span></span><br><span class="line">	buttom := <span class="built_in">len</span>(data) - <span class="number">2</span></span><br><span class="line">	<span class="comment">// 左边界</span></span><br><span class="line">	left := <span class="number">0</span></span><br><span class="line">	<span class="comment">// 顶边界</span></span><br><span class="line">	top := <span class="number">0</span></span><br><span class="line"></span><br><span class="line">	<span class="comment">// 定义行列索引</span></span><br><span class="line">	row := <span class="number">0</span></span><br><span class="line">	clo := <span class="number">0</span></span><br><span class="line"></span><br><span class="line">	<span class="keyword">for</span> left &lt; <span class="built_in">len</span>(data) &#123;</span><br><span class="line">		<span class="keyword">for</span> row == left &amp;&amp; clo &lt;= right &#123;</span><br><span class="line">			fmt.Print(data[row][clo], <span class="string">" "</span>)</span><br><span class="line">			clo++</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">for</span> clo == right+<span class="number">1</span> &amp;&amp; row &lt;= buttom &#123;</span><br><span class="line">			fmt.Print(data[row][clo], <span class="string">" "</span>)</span><br><span class="line">			row++</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">for</span> row == buttom+<span class="number">1</span> &amp;&amp; clo &gt;= left+<span class="number">1</span> &#123;</span><br><span class="line">			fmt.Print(data[row][clo], <span class="string">" "</span>)</span><br><span class="line">			clo--</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">for</span> clo == left &amp;&amp; row &gt;= top+<span class="number">1</span> &#123;</span><br><span class="line">			fmt.Print(data[row][clo], <span class="string">" "</span>)</span><br><span class="line">			row--</span><br><span class="line">		&#125;</span><br><span class="line">		row++</span><br><span class="line">		clo++</span><br><span class="line">		right--</span><br><span class="line">		top++</span><br><span class="line">		buttom--</span><br><span class="line">		left++</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

        
        <br />
        <div id="comment-container">
        </div>
        <div id="disqus_thread"></div>

        <div id="lv-container">
        </div>

    </div>
</div>

    </div>
</div>


<footer class="footer">
    <ul class="list-inline text-center">
        
        

        

        

        

        

    </ul>
    
    <p>
        <span>/</span>
        
        <span><a href="https://blog.shiniao.fun/">鸟石的博客</a></span>
        <span>/</span>
        
    </p>
    
    <p>
        <span id="busuanzi_container_site_pv">
            <span id="busuanzi_value_site_pv"></span>PV
        </span>
        <span id="busuanzi_container_site_uv">
            <span id="busuanzi_value_site_uv"></span>UV
        </span>
        Created By <a href="https://hexo.io/">Hexo</a>  Theme <a href="https://github.com/aircloud/hexo-theme-aircloud">AirCloud</a></p>
</footer>




</body>

<script>
    // We expose some of the variables needed by the front end
    window.hexo_search_path = "search.json"
    window.hexo_root = "/"
    window.isPost = true
</script>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script src="/js/index.js"></script>
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>


    <script>
        /**
         *  RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS.
         *  LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables
        */
        if( '' || '')
        var disqus_config = function () {
            this.page.url = '';  // Replace PAGE_URL with your page's canonical URL variable
            this.page.identifier = ''; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
        };

        (function() { // DON'T EDIT BELOW THIS LINE
            var d = document, s = d.createElement('script');
            s.src = 'https://airclouds-blog.disqus.com/embed.js';
            s.setAttribute('data-timestamp', +new Date());
            (d.head || d.body).appendChild(s);
        })();
    </script>



</html>
